<template>
  <!-- @成员弹窗 -->
  <div
    class="atDialog"
    :style="{
      position: 'fixed',
      top: position.y + 'px',
      left: position.x + 'px',
    }"
  >
    <div class="groupUserList">
      <div
        v-for="(item, i) in mockList"
        :key="item.user_name"
        class="item"
        :class="{ active: i === index }"
        ref="usersRef"
        @click="clickAt($event, item)"
      >
        <div class="avatar"><img :src="item.head_img" /></div>
        <div class="name">{{ item.nick_name }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "AtDialog",
  props: {
    visible: Boolean,
    position: Object,
    queryString: String,
    mockList: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      users: [],
      index: -1,
    };
  },
  watch: {},
  mounted() {
    document.addEventListener("keyup", this.keyDownHandler);
  },
  destroyed() {
    document.removeEventListener("keyup", this.keyDownHandler);
  },
  methods: {
    keyDownHandler(e) {
      if (e.code === "Escape") {
        this.$emit("onHide");
        return;
      }
      // 键盘按下 => ↓
      if (e.code === "ArrowDown") {
        if (this.index >= this.mockList.length - 1) {
          this.index = 0;
        } else {
          this.index = this.index + 1;
        }
      }
      // 键盘按下 => ↑
      if (e.code === "ArrowUp") {
        if (this.index <= 0) {
          this.index = this.mockList.length - 1;
        } else {
          this.index = this.index - 1;
        }
      }
      // 键盘按下 => 回车
      if (e.code === "Enter") {
        if (this.mockList.length) {
          const user = {
            nick_name: this.mockList[this.index].nick_name,
            user_name: this.mockList[this.index].user_name,
          };
          this.$emit("onPickUser", user);
          this.index = -1;
        }
      }
    },
    clickAt(e, item) {
      const groupUser = {
        nick_name: item.nick_name,
        user_name: item.user_name,
      };
      this.$emit("onPickUser", groupUser);
      this.index = -1;
    },
  },
  created() {},
};
</script>

<style scoped lang="scss">
.atDialog {
  .groupUserList {
    width: 200px;
    background: #fff;
    // box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
    box-sizing: border-box;
    overflow-y: auto;
    overflow-x: hidden;
    max-height: 175px;
    box-shadow: 0px 0px 14px 0px rgba(162, 162, 162, 0.5);
  }
  .groupUserList::-webkit-scrollbar {
    width: 6px;
  }
  .groupUserList::-webkit-scrollbar-track {
    border-radius: 10px;
  }
  .groupUserList::-webkit-scrollbar-thumb {
    background-color: #c1c1c1;
    border-radius: 20px;
  }
  .empty {
    font-size: 14px;
    padding: 0 20px;
    color: #999;
  }
  .item {
    font-size: 14px;
    padding: 0 0 0 5px;
    cursor: pointer;
    color: #333;
    display: flex;
    align-items: center;
    height: 35px;
    background: #fff;
    .avatar {
      flex-basis: 30px;
      img {
        width: 25px;
        height: 25px;
        object-fit: cover;
        vertical-align: bottom;
      }
    }
    &:hover,
    &.active {
      background: #ececee;
    }
    .id {
      font-size: 12px;
      color: rgb(83, 81, 81);
    }
  }
}
</style>
